package com.android.sq.zouqi.bdmap;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.sq.zouqi.R;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.ItemizedOverlay.OnFocusChangeListener;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.OverlayItem;

public class PinOverlay extends ItemizedOverlay<OverlayItem> implements OnFocusChangeListener, OnClickListener {
	private final static String TAG = "PinOverlay";
	public List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
	private MapView mapView;
	private View popView;
	private Drawable marker;
	private Context mContext;
	private MapController mapCtrl;
	private GeoPoint point = null;
	private String desc = "";
	public int layout_x = 0; // 用于设置popview 相对某个位置向x轴偏移
	public int layout_y = -45; // 用于设置popview 相对某个位置向x轴偏移
	
	private Drawable itemDrawable;
	//private Drawable itemSelectDrawable;
	//private OverlayItem selectItem;
	//private OverlayItem lastItem;
	
	private RelativeLayout button;
	public PinOverlay(Drawable marker, Context context, 
				MapView mapView, View popView, MapController mapCtrl) {
		super(boundCenterBottom(marker));
		this.marker = marker;
		this.mContext = context;
		itemDrawable = marker;
		setOnFocusChangeListener(this);
		layout_x = itemDrawable.getBounds().centerX();
		layout_y = - itemDrawable.getBounds().height();
		this.mapView = mapView;
		this.popView = popView;
		this.mapCtrl = mapCtrl;
		Log.e(TAG, "PinOverlay构造函数");
	}

	public void addOverlay(OverlayItem item) {
		//Log.e(TAG, "PinOverlay增加了一个OverlayItem");
		mGeoList.add(item);
		this.populate();
	}
	
	public void removeOverlay(int location) {
		mGeoList.remove(location);
	}
	
	public void updateOverlay()
	{
		//Log.e("ItemizedOverlay", "updateOverlay");
		populate();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {

		// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
		/*Projection projection = mapView.getProjection(); 
		for (int index = size() - 1; index >= 0; index--) { // 遍历mGeoList
			OverlayItem overLayItem = getItem(index); // 得到给定索引的item

			String title = overLayItem.getTitle();
			// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
			Point point = projection.toPixels(overLayItem.getPoint(), null); 

			// 可在此处添加您的绘制代码
			Paint paintText = new Paint();
			paintText.setColor(Color.BLUE);
			paintText.setTextSize(15);
			canvas.drawText(title, point.x-30, point.y, paintText); // 绘制文本
		}*/
		//Log.e(TAG, "super.draw之前");
		//print(canvas, mapView, shadow);
		super.draw(canvas, mapView, shadow);
		//Log.e(TAG, "PinOverlay->onDraw");
		//调整一个drawable边界，使得（0，0）是这个drawable底部最后一行中心的一个像素
		boundCenterBottom(marker);
		
	}

	public void print(Canvas canvas, MapView mapView, boolean shadow) {
		if (mapView == null) {
			Log.e(TAG, "mapView是空的");
		}
		if (shadow) {
			Log.e(TAG, "shadow为true");
		} else {
			Log.e(TAG, "shadow为false");
		}
		if (canvas == null) {
			Log.e(TAG, "Canvas为空");
		}
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		//Log.e("ItemizedOverlay", "createItem");
		return mGeoList.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mGeoList.size();
	}

	
	@Override
	// 处理当点击事件
	protected boolean onTap(int i) {
		popView.setVisibility(View.VISIBLE);
		return super.onTap(i);
	}

	@Override
	public boolean onTap(GeoPoint arg0, MapView arg1) {
		// TODO Auto-generated method stub
		// 消去弹出的气泡
		popView.setVisibility(View.GONE);
		return super.onTap(arg0, arg1);
	}

	@Override
	public void onFocusChanged(ItemizedOverlay<?> overlay, OverlayItem newFocus) {
		// TODO Auto-generated method stub
		//Log.e(TAG , "有新popView-newFocus = " + newFocus);
		if (null != newFocus) {
			MapView.LayoutParams params = (MapView.LayoutParams) popView.getLayoutParams();
			params.x = this.layout_x;	//Y轴偏移
			params.y = this.layout_y;	//Y轴偏移
			point = newFocus.getPoint();
			params.point = point;
			mapView.updateViewLayout(popView,
	                new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
	                		point, MapView.LayoutParams.BOTTOM_CENTER));
			
			mapCtrl.animateTo(point);
			TextView title_TextView = (TextView) popView.findViewById(R.id.loc_title);
			title_TextView.setText(newFocus.getTitle());
			TextView desc_TextView = (TextView) popView.findViewById(R.id.loc_text);
			if(null == newFocus.getSnippet() || "".equals(newFocus.getSnippet())){
				desc_TextView.setVisibility(View.GONE);
			}else{
				desc = newFocus.getSnippet();
				desc_TextView.setText(desc);
				desc_TextView.setVisibility(View.VISIBLE);
			}
			button = (RelativeLayout) popView.findViewById(R.id.loc_btn);
			button.setOnClickListener(this);
			mapView.updateViewLayout(popView, params);
			popView.setVisibility(View.VISIBLE);
			//selectItem = newFocus;
		}
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.loc_btn:
			Log.e(TAG, "点击了popView");
			/*
			 * 考虑添加详细的地址，因为有些地方从百度服务器获取的地址只到区、或者什么路而已，并不详细
			 */
			break;
		}
	}
}